<?php
namespace App\Library;

use Phalcon\DI;
use App\Library\MyCrypt;

class App {
    /**
     * @var \Phalcon\Di
     */
    private static $di;

    /**
     * @var \Phalcon\Logger\AdapterInterface
     */
    private static $logger;

    /**
     * 设置Di对象
     *
     * @param \Phalcon\Di $di
     */
    public static function setDI($di) {
        if(self::$di == null) {
            self::$di = $di;
        }
    }

    /**
     * 获取DI对象
     *
     * @return \Phalcon\Di
     */
    public static function getDI() {
        if(self::$di == null) {
            self::$di = (new DI())->getDefault();
        }
        return self::$di;
    }

    /**
     * 获取当前用户的uid
     *
     * @return bool
     */
    public static function getCurrentUserUid() {
        if(isset($_COOKIE) && isset($_COOKIE['uid'])) {
            $myCrypt = new MyCrypt();
            $uid     = $myCrypt->decrypt(urldecode($_COOKIE['uid']));
            if(is_numeric($uid)) {
                return intval($uid);
            }
        }
        return false;
    }

    /**
     * 设置当前登录用后的cookie
     *
     * @param int $uid 用户的uid
     *
     */
    public static function setCurrentUserUid($uid) {
        $myCrypt = new MyCrypt();
        $str     = $myCrypt->encrypt($uid);
        setcookie('uid', urlencode($str), time() + 365 * 24 * 60 * 60, '/');
    }

    /**
     * 返回日志操作的功能
     * 使用方法
     * App::Logger()->error('msg');
     * App::Logger()->info('msg');
     * App::Logger()->notice('msg');
     * App::Logger()->warning('msg');
     * App::Logger()->debug('msg');
     *
     * @return \Phalcon\Logger\AdapterInterface
     */
    public static function Logger() {
        if(self::$logger == null) {
            self::$logger = self::getDI()->getShared('logger');
        }
        return self::$logger;
    }

    /**
     * @param string $key 配置的索引
     *
     * @return \Phalcon\Config
     */
    public static function globalConfig($key = '') {
        $globalConfig = self::getDI()->getShared('globalConfig');
        if($key == '') {
            return $globalConfig;
        }
        return $globalConfig->get($key);
    }

    /**
     * 显示错误信息
     *
     * @param \Phalcon\Exception $ex
     */
    public static function showError($ex) {
        if(extension_loaded('xdebug')) {
            echo '<table>' . $ex->xdebug_message . '</table>';
            exit();
        }
        $dispatcher = self::getDI()->getShared('dispatcher');
        $dispatcher->setNamespaceName('App\Controllers\\');
        $dispatcher->setControllerName('Exception');
        $dispatcher->setActionName('showError');
        $dispatcher->setParams([
            'message' => $ex->getMessage(),
            'file'    => $ex->getFile(),
            'code'    => $ex->getCode(),
            'line'    => $ex->getLine(),
            'trace'   => $ex->getTraceAsString(),
        ]);
        $dispatcher->dispatch();
    }

    /**
     * 显示错误信息
     *
     * @param \PDOException $ex
     */
    public static function dbError($ex) {
        if(extension_loaded('xdebug')) {
            echo '<table>' . $ex->xdebug_message . '</table>';
            exit();
        }
        $dispatcher = self::getDI()->getShared('dispatcher');
        $dispatcher->setNamespaceName('App\Controllers\\');
        $dispatcher->setControllerName('Exception');
        $dispatcher->setActionName('showError');
        $dispatcher->setParams([
            'message' => $ex->getMessage(),
            'file'    => $ex->getFile(),
            'code'    => $ex->getCode(),
            'line'    => $ex->getLine(),
            'trace'   => $ex->getTraceAsString(),
        ]);
        $dispatcher->dispatch();
    }
}
